using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using BA.DAL;
using BA;

namespace BlazeApps.Messaging
{
    [Serializable()]
    public class PrivateMessageParent : Csla.BusinessListBase<PrivateMessageParent, PrivateMessageChild>
    {
        public enum ListType
        {Inbox = 0, Sent = 1};

        #region BindingList Overrides
        protected override object AddNewCore()
        {
            PrivateMessageChild item = PrivateMessageChild.NewPrivateMessageChild();
            this.Add(item);
            return item;
        }
        #endregion //BindingList Overrides

        #region Authorization Rules

        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in PrivateMessageParent
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PrivateMessageParentViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in PrivateMessageParent
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PrivateMessageParentAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in PrivateMessageParent
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PrivateMessageParentEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in PrivateMessageParent
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PrivateMessageParentDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private PrivateMessageParent()
        { /* require use of factory method */
        }

        public static PrivateMessageParent NewPrivateMessageParent()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a PrivateMessageParent");
            return new PrivateMessageParent();
        }

        public static PrivateMessageParent GetPrivateMessageParent()
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a PrivateMessageParent");
            return DataPortal.Fetch<PrivateMessageParent>(new FilterCriteria());
        }

        public static PrivateMessageParent GetPrivateMessageParent(ListType ViewType, Guid UserID)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a PrivateMessageParent");
            return DataPortal.Fetch<PrivateMessageParent>(new FilterCriteria(ViewType, UserID));
        }

        #endregion //Factory Methods

        #region Data Access

        #region Filter Criteria
        [Serializable()]
        private class FilterCriteria
        {
            public ListType ViewType;
            public Guid UserID;

            public FilterCriteria()
            {

            }

            public FilterCriteria(ListType ViewType, Guid UserID)
            {
                this.ViewType = ViewType;
                this.UserID = UserID;
            }
        }
        #endregion //Filter Criteria

        #region Data Access - Fetch
        private void DataPortal_Fetch(FilterCriteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(FilterCriteria criteria)
        {
            BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
            BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);
            string DataProc = "";

            if (criteria.ViewType == ListType.Inbox)
            {
                DataProc = "usp_private_message_inboxitems";
                DAL.AddParameter("@userid", criteria.UserID);
            }

            if (criteria.ViewType == ListType.Sent)
            {
                DataProc = "usp_private_message_sentitems";
                DAL.AddParameter("@userid", criteria.UserID);
            }

            using (SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(DataProc, CommandType.StoredProcedure, ConnectionState.Open)))
            {
                while (dr.Read())
                    this.Add(PrivateMessageChild.GetPrivateMessageChild(dr));
            }
        }
        #endregion //Data Access - Fetch

        #region Data Access - Update
        protected override void DataPortal_Update()
        {
            //RaiseListChangedEvents = false;

            //using (SqlConnection cn = new SqlConnection(Database.BlazeKBConnection))
            //{
            //    cn.Open();

            //    // loop through each deleted child object
            //    foreach (PrivateMessageChild deletedChild in DeletedList)
            //        deletedChild.DeleteSelf(cn);
            //    DeletedList.Clear();

            //    // loop through each non-deleted child object
            //    //foreach (PrivateMessageChild child in this)
            //    //{
            //    //    if (child.IsNew)
            //    //        child.Insert(cn);
            //    //    else
            //    //        child.Update(cn);
            //    //}
            //}//using SqlConnection

            //RaiseListChangedEvents = true;
        }
        #endregion //Data Access - Update

        #endregion //Data Access
    }
}
